import Vue from 'vue';
import { login } from '@/mock'
// import { CACHE_INCLUDED_ROUTES } from '@/store/mutation-types'
import { queryPermissionsByUser } from '@/mock'
const user = {
    state: {
        // 用户相关的状态信息
        token: '',
        username:'',
        realname: '',
        tenantid: '',
        welcome: '',
        avatar: '',
        permissionList: [],
        info: {}
    },
    mutations: {
        SET_TOKEN: (state,token) => {
            state.token = token;
        },
        SET_NAME: (state,{username,realname,welcome}) => {
            state.username = username;
            state.realname = realname;
            state.welcome = welcome;
        },
        SET_PERMISSIONLIST: (state,permissionList)=>{
            state.permissionList = permissionList;
        }
    },
    actions: {
        // 登录
        Login({commit}) { 
            return new Promise((resolve,reject) => { 
                login().then(response => {
                    console.log('response',response);
                    if(response.code == '200') {
                        const result = response.result;
                        const userInfo = result.userInfo
                        Vue.ls.set('ACCESS_TOKEN', result.token, 7 * 24 * 60 * 60 * 1000)
                        Vue.ls.set('USER_NAME', userInfo.username, 7 * 24 * 60 * 60 * 1000)
                        Vue.ls.set('USER_INFO', userInfo, 7 * 24 * 60 * 60 * 1000)
                        commit('SET_TOKEN', result.token)
                        commit('SET_INFO', userInfo)
                        commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname })
                        commit('SET_AVATAR', userInfo.avatar)
                        resolve(response)
                    }else {
                        reject(response)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 获取用户信息
        GetPermissionList({ commit }) {
            return new Promise((resolve, reject) => {
                queryPermissionsByUser().then(response => {
                    console.log('response',response);
                    const menuData = response.result.menu;
                    if (menuData && menuData.length > 0) { 
                        commit('SET_PERMISSIONLIST', menuData)
                    }
                    resolve(response)
                }).catch(error => {
                    reject(error)
                  })
            })
        }

        // 登出
        // Logout({ commit }) { 
        //     return new Promise((resolve) => {
        //         // let logoutToken = state.token;
        //         commit('SET_TOKEN', '');
        //         commit('SET_PERMISSIONLIST', '');
        //         Vue.ls.remove(CACHE_INCLUDED_ROUTES);
        //         resolve();
        //      },)
        // }
    }
}

export default user;